<template>
  <div>
    <!-- 按钮 -->

    <el-button style="margin:10px 10px" type="primary" icon="el-icon-refresh-right" circle
      @click="getDynamicExtensionProcessList"></el-button>
    <el-button type="primary" @click="add()">添加</el-button>

    <!-- <el-button type="primary" icon="el-icon-plus" style="margin:10px" @click="getShow">添加</el-button> -->
    <!-- 表格
          data 展示数据 —— 数组类型
        -->
    <el-table style="width: 100%;" border :data="list">
      <el-table-column prop="content" label="内容" align="center">
      </el-table-column>
      <!--       <el-table-column prop="estimatedTime" label="通知范围" align="center">
            </el-table-column> -->
      <!-- <el-table-column prop="picUrl" label="图片地址" align="center">
            </el-table-column> -->

      <el-table-column prop="linkType" label="链接类型" align="center">
        <template slot-scope="{ row, $index }">
          <div>{{ row.linkType == "1" ? '内部链接' : '外部链接' }}</div>
        </template>
      </el-table-column>

      <el-table-column prop="linkUrl" label="链接地址" align="center">
      </el-table-column>

      <!-- 多选城市 -->
      <el-table-column prop="city" label="通知城市" align="center">
      </el-table-column>

      <el-table-column prop="sexType" label="通知范围" align="center">
        <template slot-scope="{ row, $index }">
          <div v-if="row.sexType == 1">男</div>
          <div v-else-if="row.sexType == 2">女</div>
          <div v-else>所有人</div>
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="时间" align="center">
      </el-table-column>

      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="{ row, $index }">
          <!-- <el-tooltip content="修改套餐" placement="top" slot="reference">
                        <el-button type="primary" icon="el-icon-edit" @click="ubpateBannar(row)"
                            :disabled="!list.isCustom ? false : true"></el-button>
                    </el-tooltip> -->
          <el-popconfirm title="是否删除？" icon="el-icon-info" icon-color="red" @confirm="getDelete(row.id)">
            <el-tooltip content="删除" placement="top" slot="reference">
              <el-button type="danger" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination style="margin-top:20px;text-align:center" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" :current-page="pageNumber" :page-sizes="[10, 50, 100]" :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 对话框  -->
    <el-dialog title="添加通知" :visible.sync="dialogFormVisible">
      <el-form style="width:80%" :model="tmFrom" :rules="rules" ref="ruleForm">
        <el-form-item label="内容" label-width="100px" prop="content">
          <el-input autocomplete="off" v-model="tmFrom.content"></el-input>
        </el-form-item>

        <!--       <el-form-item label="链接类型" label-width="100px" prop="linkType">
                    <el-input autocomplete="off" v-model="tmFrom.linkType"></el-input>
                </el-form-item> -->
        <el-form-item label="链接类型" label-width="100px">
          <el-select v-model="tmFrom.linkType" placeholder="请选择链接类型" prop="linkType">
            <el-option label="内部链接" value="1"></el-option>
            <el-option label="外部链接" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-if="tmFrom.linkType == 2" label="链接地址" label-width="100px" prop="linkUrl">
          <el-input autocomplete="off" v-model="tmFrom.linkUrl"></el-input>
        </el-form-item>

        <el-form-item v-if="tmFrom.linkType == 1" label="链接地址" label-width="100px" prop="linkUrl">
          <el-select v-model="tmFrom.linkUrl">
            <el-option v-for="v in FrontPageUrlConfigurationList" :key="v.id" :value="v.pageUrl" :label="v.pageName">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否置顶通知" label-width="100px">
          <el-select v-model="tmFrom.isTop" placeholder="请选择是否置顶通知" prop="sexType">
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="通知城市" label-width="100px">
          <el-cascader v-model="tmFrom.city" :options="citys"
            :props="{ multiple: true, label: 'label', value: 'label' }" clearable></el-cascader>
        </el-form-item>

        <el-form-item label="通知范围" label-width="100px">
          <el-select v-model="tmFrom.sexType" placeholder="请选择通知范围" prop="sexType">
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
            <el-option label="所有人" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="getAddBannerImage" v-if="state">确 定</el-button>
        <el-button type="primary" @click="UpdateBillingRules" v-else>修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import citys from "@/assets/Cascader.js"
console.log(citys, "???????");
export default {
  name: 'Article',
  data() {

    return {
      citys,
      //页码
      pageNumber: 1,
      //每页数量
      pageSize: 10,
      //总条数
      total: 0,
      list: [],
      //列表数据
      FrontPageUrlConfigurationList: [],
      // 对话框显示与隐藏控制
      dialogFormVisible: false,
      // 表单对象
      tmFrom: {
        // name: '',
        // proportion: '',
        // intro: ''
        city: '',
        adUserIds: "",
        content: "",
        isTop: "",
        linkType: "",
        linkUrl: "",
        receiveType: 1,
        sexType: "",
      },
      // 当前为修改还是添加 修改为false 添加为true
      state: true,
      // 表单验证
      rules: {
        // name: [{
        //   required: true,
        //   message: '请输入规则名称',
        //   trigger: 'blur'
        // }],
        // proportion: [{
        //     required: true,
        //     message: '请选择计费比列，数字和小数',
        //     trigger: 'blur'
        //   },
        //   {
        //     transform(value) {
        //       return Number(value);
        //     },
        //     validator(rule, value, callback) {
        //       if (Number.isFinite(value) && value > 0) {
        //         callback();
        //       } else {
        //         callback(new Error("请输入类型为数字和小数，并不少于0"));
        //       }
        //     },
        //     trigger: "blur",
        //   }
        // ],
        // intro: [{
        //   required: true,
        //   message: '请选择规则简介',
        //   trigger: 'blur'
        // }]
      }
    }
  },
  mounted() {
    this.getNotificationManagementPageList()
    this.getFrontPageUrlConfigurationList()
  },

  methods: {
    // 获取app页面列表
    async getFrontPageUrlConfigurationList() {
      let res = await this.$API.system.getFrontPageUrlConfigurationList()
      console.log(res, "获取app页面列表")
      this.FrontPageUrlConfigurationList = res.data;
    },

    async getNotificationManagementPageList() {
      let res = await this.$API.system.getNotificationManagementPageList({
        pageNumber: this.pageNumber,
        pageSize: this.pageSize,
        orderBY: ''
      })
      console.log(res, "通知列表")
      this.total = res.data.sumLine;
      this.list = res.data.rows;
    },



    // 取消
    getShow() {
      this.state = true
      this.dialogFormVisible = true
      this.tmFrom = {
        // name: '',
        // proportion: '',
        // intro: ''
      }
    },
    // 刷新
    getDynamicExtensionProcessList() {
      this.getNotificationManagementPageList()
    },
    // 添加一条
    add() {
      this.dialogFormVisible = !this.dialogFormVisible
      this.tmFrom = {
        // name: '',
        // proportion: '',
        // intro: ''
      }

    },
    //确认 添加
    getAddBannerImage() {

      let city = []
      this.tmFrom.city.forEach(element => {
        city.push(element[element.length - 1])
      });
      this.tmFrom.city = city
      this.tmFrom.receiveType = 1
      this.tmFrom.adUserIds = ''
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$API.system.addNotificationManagement(this.tmFrom);
          this.dialogFormVisible = false;
          this.getNotificationManagementPageList();
          console.log(this.getNotificationManagementPageList(), '数据格式');
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },


    // 删除
    async getDelete(id) {
      let res = await this.$API.system.deleteNotificationManagement({
        id: id
      });
      this.getNotificationManagementPageList();
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val
      this.getNotificationManagementPageList()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNumber = val
      this.getNotificationManagementPageList()
    },
  }
}
</script>

<style lang="scss">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
